package cn.windy.samary.module.admin.controller;

import cn.windy.samary.core.annotation.OpLog;
import cn.windy.samary.module.admin.common.Tree;
import cn.windy.samary.module.admin.entity.Dept;
import cn.windy.samary.module.admin.service.IDeptService;
import cn.windy.samary.module.abstracts.controller.AbstractController;
import cn.windy.samary.utils.PageContent;
import cn.windy.samary.utils.Res;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author zyl
 * @date 2018-05-26 16:06
 */
@Controller
@RequestMapping("/admin/dept")
public class DeptController extends AbstractController {

    private String prefix = "admin/dept/";

    @Resource
    private IDeptService deptService;

    @GetMapping
    @OpLog(value = "打开部门管理主页")
    @RequiresPermissions("admin:dept:dept")
    public String index(HttpServletRequest request, Model model) {
        return "admin/dept/dept";
    }

    @ResponseBody
    @GetMapping(value = "/list")
    @OpLog(value = "查询部门列表")
    @RequiresPermissions("admin:dept:dept")
    public List<Dept> list(PageContent pageContent,
                           String username, String nickname) {
        Example example = new Example(Dept.class);
        Example.Criteria criteria = example.createCriteria();
        example.orderBy("sort");
        return deptService.selectByExample(example);
    }

    @OpLog("添加部门")
    @GetMapping("add/{pId}")
    @RequiresPermissions("admin:dept:add")
    public String add(@PathVariable String pId, Model model) {
        model.addAttribute("pId", pId);
        if ("0".equals(pId)) {
            model.addAttribute("pName", "根节点");
        } else {
            model.addAttribute("pName", deptService.selectByPrimaryKey(pId).getName());
        }
        return "admin/dept/deptAdd";
    }

    @OpLog("编辑部门")
    @GetMapping("edit/{id}")
    @RequiresPermissions("admin:dept:edit")
    public String edit(HttpServletRequest request, Model model,
                       @PathVariable String id) {
        model.addAttribute("basePath", getBasePath(request));
        Dept dept = deptService.selectByPrimaryKey(id);
        if (StringUtils.isBlank(dept.getpId()) || "0".equals(dept.getpId())) {
            model.addAttribute("pName", "无");
        } else {
            Dept parDept = deptService.selectByPrimaryKey(dept.getpId());
            model.addAttribute("pName", parDept.getName());
        }
        model.addAttribute("dept", dept);
        return "admin/dept/deptEdit";
    }


    @ResponseBody
    @OpLog("保存部门")
    @PostMapping("save")
    @RequiresPermissions("admin:dept:add")
    public Object save(@Valid Dept entity) {
        int i = deptService.insertSelective(entity);
        return i != 0 ? Res.ok() : Res.error();
    }

    @ResponseBody
    @OpLog("更新部门")
    @PutMapping("update")
    @RequiresPermissions("admin:dept:edit")
    public Object update(@Valid Dept entity) {
        int i = deptService.updateByPrimaryKeySelective(entity);
        return i != 0 ? Res.ok() : Res.error();
    }

    @OpLog("删除部门")
    @ResponseBody
    @DeleteMapping("remove")
    @RequiresPermissions("admin:dept:remove")
    public Object remove(String id) {
        Dept dept = new Dept();
        dept.setpId(id);
        List<Dept> select = deptService.select(dept);
        if (select.size() != 0) {
            return Res.error("包含下级部门，不能直接删除！");
        }
        int i = deptService.deleteByPrimaryKey(id);
        return i != 0 ? Res.ok() : Res.error();
    }

    @OpLog("批量删除部门")
    @ResponseBody
    @DeleteMapping("batchRemove")
    @RequiresPermissions("admin:dept:batchRemove")
    public Object batchRemove(@RequestParam(value = "ids[]") String[] ids) {
        List<String> idsList = new ArrayList<>(Arrays.asList(ids));
        if (idsList.size() != 0) {
            deptService.deleteByPrimaryKeys(idsList);
        }
        return Res.ok();
    }

    @GetMapping("/tree")
    @ResponseBody
    public Tree<Dept> tree() {
        Tree<Dept> tree = new Tree<Dept>();
        tree = deptService.getTree();
        return tree;
    }

    @GetMapping("/treeView")
    public String treeView(String deptId, Model model) {
        if (StringUtils.isNotBlank(deptId)) {
            model.addAttribute("sourceDeptId", deptId);
        }
        return "admin/dept/deptTree";
    }

    @GetMapping("/isChild")
    @ResponseBody
    public boolean isChild(String deptId, String pId) {
        return deptService.isChild(deptId, pId);
    }
}
